// pages/recharge-history/recharge-history.js
Page({
  data: {
    showTimeModal: false, // 是否显示时间选择弹窗
    selectedYear: '2025', // 选中的年份
    selectedMonth: '', // 选中的月份
    yearOptions: ['2025', '2024', '2023', '2022'], // 年份选项
    monthOptions: ['全部', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'], // 月份选项
    rechargeRecords: [], // 充值记录列表
    filteredRecords: [], // 筛选后的记录
    currentPage: 1,
    pageSize: 10,
    hasMore: true
  },

  onLoad(options) {
    // 初始化数据
    this.initData();
  },

  // 初始化数据
  initData() {
    // 模拟充值记录数据
    const mockRecords = [
      {
        id: 1,
        date: '2025-01-15 14:30:25',
        amount: 1000.00,
        status: '成功',
        orderNo: 'CZ202501150001',
        paymentMethod: '微信支付'
      },
      {
        id: 2,
        date: '2025-01-10 09:15:30',
        amount: 2000.00,
        status: '成功',
        orderNo: 'CZ202501100002',
        paymentMethod: '微信支付'
      },
      {
        id: 3,
        date: '2024-12-28 16:45:12',
        amount: 500.00,
        status: '成功',
        orderNo: 'CZ202412280003',
        paymentMethod: '微信支付'
      },
      {
        id: 4,
        date: '2024-12-20 11:20:45',
        amount: 1500.00,
        status: '成功',
        orderNo: 'CZ202412200004',
        paymentMethod: '微信支付'
      },
      {
        id: 5,
        date: '2024-11-15 13:10:20',
        amount: 3000.00,
        status: '成功',
        orderNo: 'CZ202411150005',
        paymentMethod: '微信支付'
      }
    ];

    this.setData({
      rechargeRecords: mockRecords,
      filteredRecords: mockRecords
    });
  },

  // 显示时间选择弹窗
  showTimeSelector() {
    this.setData({
      showTimeModal: true
    });
  },

  // 关闭时间选择弹窗
  closeTimeModal() {
    this.setData({
      showTimeModal: false
    });
  },

  // 选择年份
  selectYear(e) {
    const year = e.currentTarget.dataset.year;
    this.setData({
      selectedYear: year
    });
    this.filterRecords();
  },

  // 选择月份
  selectMonth(e) {
    const month = e.currentTarget.dataset.month;
    this.setData({
      selectedMonth: month
    });
    this.filterRecords();
  },

  // 筛选记录
  filterRecords() {
    let filtered = this.data.rechargeRecords;
    
    // 按年份筛选
    if (this.data.selectedYear) {
      filtered = filtered.filter(record => {
        return record.date.startsWith(this.data.selectedYear);
      });
    }
    
    // 按月份筛选
    if (this.data.selectedMonth && this.data.selectedMonth !== '全部') {
      filtered = filtered.filter(record => {
        const month = record.date.substring(5, 7);
        return month === this.data.selectedMonth;
      });
    }

    this.setData({
      filteredRecords: filtered
    });
  },

  // 确认时间选择
  confirmTimeSelection() {
    this.closeTimeModal();
  },

  // 重置筛选
  resetFilter() {
    this.setData({
      selectedYear: '2025',
      selectedMonth: '',
      filteredRecords: this.data.rechargeRecords
    });
  },

  // 查看记录详情
  viewRecordDetail(e) {
    const recordId = e.currentTarget.dataset.id;
    wx.showToast({
      title: '记录详情功能开发中',
      icon: 'none',
      duration: 2000
    });
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.initData();
    wx.stopPullDownRefresh();
  },

  // 上拉加载更多
  onReachBottom() {
    if (this.data.hasMore) {
      // 模拟加载更多数据
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: 2000
      });
    }
  },

  // 阻止事件冒泡
  stopPropagation() {
    // 阻止事件冒泡
  }
});
